﻿using HBase;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
namespace hconnect {
    public abstract class SiemensConnectSimotion :SiemensConnect {
        public int initfinsh = 0;



        private static string key;
        private static string key3;

        static SiemensConnectSimotion() {
            string tpkt1 = "03 00 00 16";
            String length = "11";
            String pudType = "e0";
            String destination = "00 00";
            String source = "00 15";
            String srcTsap = iso("c1" ,"00 01");
            String desTsap = iso("c2" ,"02 02");
            String tpduSize = iso("c0" ,"0a");
            key = tpkt1 + string.Format(" {0} {1} {2} {3} 00 {4} {5} {6}" ,length ,pudType ,destination ,source ,srcTsap ,desTsap ,tpduSize);
            string tpkt3 = "03 00 00 19";
            String iso8073 = "02 f0 80";
            String head3 = "32 01 00 00 ff ff 00 08 00 00";
            String param = "f0 00 00 02 00 02 01 e0";
            key3 = tpkt3 + string.Format(" {0} {1} {2}" ,iso8073 ,head3 ,param);

        }

        static String iso(String type ,String val) {
            int len = val.Split(' ').Length;
            String l = len.ToString("X2");
            return string.Format("{0} {1} {2}" ,type ,l ,val);
        }

        public override bool afterConnect() {

            byte[] b = Siemens102Util.StrToArraybyte(key);
            Send(b);

            byte[] buffer = ReadTelex(1024);
            int num = buffer.Length;
            if( num == 22 ) {

                byte[] b2 = Siemens102Util.StrToArraybyte(key3);
                Send(b2);
                byte[] buffer2 = ReadTelex(1024);
                num = buffer2.Length;
                if( num == 27 ) {
                    initfinsh = 1;
                    return true;
                }
            } else {
                fileAll("请检查端口" + port + "与IP" + ip);
            }
            return false;
        }


    }
}
